package servlet;

import dao.Dao;
import domain.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/updateGoodNumberServlet")
public class UpdateGoodNumberServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String sid = request.getParameter("sid");
        String suserid = request.getParameter("suserid");
        String str = request.getParameter("str");
        Dao dao = new Dao();
        Goods goodByCid = dao.findGoodByCid(sid);
        if (goodByCid.getCnumber() < Integer.valueOf(str)) {
            request.setAttribute("updateGoodNumberInfo", "商品数量不足，请重新输入");
            request.getRequestDispatcher("/ShoppingCarList.jsp?id=" + suserid).forward(request,response);
        } else {
            dao.updateGoodNumber(sid, Integer.valueOf(suserid), Integer.valueOf(str));
            response.sendRedirect(request.getContextPath() + "/ShoppingCarList.jsp?id=" + suserid);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
